package models

import (
	"fmt"
	"time"

	"github.com/beego/beego/v2/client/orm"
)

type TAchievment struct {
	Id        int       `orm:"column(id);auto"`
	Label     string    `orm:"column(label);size(45);null" description:"标题"`
	Desc      string    `orm:"column(desc);size(145);null" description:"描述"`
	GetDate   time.Time `orm:"column(get_date);type(date);null" description:"获证时间"`
	Creattime time.Time `orm:"column(creattime);type(datetime);null" description:"发布时间"`
	UserSN    string    `orm:"column(user_sn)" description:"学生id"`
	Img       string    `orm:"column(img);size(45);null" description:"证书url地址"`
	Level     string    `orm:"column(level);size(45);null" description:"国家级》省级》市级》校级》院级》班级》普通级"`
	Kind      string    `orm:"column(kind);size(45);null" description:"学业学术类,社会实践类、文体活动类、技能与职业资格类、品德与综合荣誉类 1. 学业学术类  奖学金证书：这是对学生学习成绩和综合表现的高度认可。如国家奖学金,用于奖励特别优秀的学生,金额较高,荣誉度高；学校设立的一、二、三等奖学金,依据学生的"`
	ImgData   string    `orm:"-"`
}

func (t *TAchievment) TableName() string {
	return "t_achievment"
}

func init() {
	orm.RegisterModel(new(TAchievment))
}

// AddTAchievment insert a new TAchievment into database and returns
// last inserted Id on success.
func AddTAchievment(m *TAchievment) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

// GetTAchievmentById retrieves TAchievment by Id. Returns error if
// Id doesn't exist
func GetTAchievmentById(id int) (v *TAchievment, err error) {
	o := orm.NewOrm()
	v = &TAchievment{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// GetTAchievmentById retrieves TAchievment by Id. Returns error if
// Id doesn't exist
func GetTAchievmentBySn(sn string) (result []*TAchievment, err error) {
	o := orm.NewOrm()
	qs := o.QueryTable(new(TAchievment))
	qs = qs.Filter("user_sn", sn)
	_, err = qs.All(&result)
	return
}

// UpdateTAchievment updates TAchievment by Id and returns error if
// the record to be updated doesn't exist
func UpdateTAchievmentById(m *TAchievment) (err error) {
	o := orm.NewOrm()
	v := TAchievment{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteTAchievment deletes TAchievment by Id and returns error if
// the record to be deleted doesn't exist
func DeleteTAchievment(id int) (err error) {
	o := orm.NewOrm()
	v := TAchievment{Id: id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&TAchievment{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}
